using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;

namespace IQIGame.Onigao.Game.Editors
{
    public class CfgFSMStateNode : EdtCfgFSMNode
    {
        public bool isEntry;
        public List<CfgFSMConditionNode> ConditionNodes;
        public string name { get; private set; }
        public List<CfgFSMStateParam> customParams;

        public override void Init(XmlElement element, string fsmShortPath)
        {
            base.Init(element, fsmShortPath);
            if (string.IsNullOrEmpty(_text))
            {
                Debug.LogError("Action节点不能为空。");
                return;
            }
            string[] texts = _text.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
            name = texts[0];
            if (texts.Length > 1)
            {
                customParams = new List<CfgFSMStateParam>();
                for (int i = 1; i < texts.Length; i++)
                {
                    customParams.Add(new CfgFSMStateParam(texts[i]));
                }
            }
        }

        public CFSMStateCfg ToConfig(Dictionary<string, int> intIdMap)
        {
            CFSMStateCfg cfg = new CFSMStateCfg();
            cfg.id = intIdMap[this.id];
            cfg.name = this.name;
            cfg.conditions = new List<int>();
            if (this.ConditionNodes != null)
            {
                foreach (var condNode in this.ConditionNodes)
                {
                    cfg.conditions.Add(intIdMap[condNode.id]);
                }
            }
            cfg.customParams = new List<CFSMStateCfg.NamedParam>();
            if (this.customParams != null)
            {
                foreach (var stateParam in this.customParams)
                {
                    var p = new CFSMStateCfg.NamedParam();
                    p.nameType = (int)stateParam.actionParamType;
                    p.type = (CFSMParamType)EdtCfgFSMUtil.ConvertType(stateParam.paramType);
                    p.value = stateParam.paramValue;
                    cfg.customParams.Add(p);
                }
            }
            return cfg;
        }

        public void GenCode(string rootPath, string privateDirPath, string entityClassName)
        {
            if (!Directory.Exists(privateDirPath))
            {
                Directory.CreateDirectory(privateDirPath);
            }
            string codeName = GetCodeFileName(out var codeModuleName);
            string codeFilePath = $"{privateDirPath}/{codeName}.cs";
            string entrySetDataStr = $"fsm.SetData(new CFSM{codeModuleName}Data());";
            string codeContent = $@"using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{{
    public class {codeName} : CFSMState<{entityClassName}, CFSM{codeModuleName}Data>
    {{
        public override void OnInit()
        {{
            {(isEntry ? entrySetDataStr : string.Empty)}
        }}

        public override void OnEnter(int prevStateId)
        {{

        }}

        public override void OnUpdate(float deltaTime)
        {{

        }}

        public override void OnExit()
        {{

        }}

        public override void OnReset()
        {{
            base.OnReset();
        }}
    }}
}}
";
            if (!File.Exists(codeFilePath))
            {
                File.WriteAllText(codeFilePath, codeContent);
                UnityEngine.Debug.Log($"生成了代码：{Path.GetFileName(codeFilePath)}");
            }
        }

        public string GetCodeFileName(out string codeModuleName)
        {
            codeModuleName = _fsmShortPath.Replace("/", "_");
            string codeName = $"{EdtCFSMConst.StateCSFilePrefix}_{codeModuleName}_{name}";
            return codeName;
        }
    }
}